var root = {
  val: 3,
  left: {
    val: 9
  },
  right: {
    val: 20,
    left: {
      val: 15
    },
    right: {
      val: 7
    }
  }
}

/**
 * 使用递归的方式
 * @param {TreeNode} root
 * @return {number[]}
 */
var maxDepth = function(root) {
  var res = 0; // 结果集
  var queue = []; // 跟踪队列
  if (root != null) {
    queue.push(root);
  }
  while (queue.length > 0) {
    var n = queue.length;
    for (var i = 0; i < n; i++) {
      var node = queue.shift();
      if (node.left != null) {
        queue.push(node.left)
      }
      if (node.right != null) {
        queue.push(node.right);
      }
    }
    res++;
  }
  return res;
};

console.log(maxDepth(root))